﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls.Descriptions;
using Jayden.Dll.Web.HtmlControls.Generics;

namespace Jayden.Dll.Web.HtmlControls
{
	public abstract class TrContainerElement : HtmlElement
	{
		public TrContainerElement(HtmlElementDescription description) : base(description) { }

		public Td[] AddTr(params string[] cells)
		{
			Tr tr = AddControl<Tr>();
			List<Td> tds = new List<Td>();
			for (int c = 0; c < cells.Length; c++)
			{
				Td td = new Td();
				td.AddText(cells[c]);
				tr.AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}
		public Td[] AddTr(params HtmlControl[] controls)
		{
			Tr tr = AddControl<Tr>();
			List<Td> tds = new List<Td>();
			for (int c = 0; c < controls.Length; c++)
			{
				Td td = new Td();
				td.AddControl(controls[c]);
				tr.AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}
		public Tr AddTr(params Td[] cells)
		{
			Tr tr = AddControl<Tr>();
			for (int c = 0; c < cells.Length; c++)
				tr.AddControl(cells[c]);
			return tr;
		}
		public Tr AddTr() { return AddControl(new Tr()) as Tr; }
	}

	public class Table : TrContainerElement
	{
		public Table() : base(TableDescription.Instance) { }

		public Caption AddCaption(string text) { return AddControl(new Caption(text)) as Caption; }

		public ColGroup AddColGroup() { return AddControl<ColGroup>(); }

		public THead AddTHead() { return AddControl<THead>(); }
		public TBody AddTBody() { return AddControl<TBody>(); }
		public TFoot AddTFoot() { return AddControl<TFoot>(); }
	}
	public class TableDescription : HtmlElementDescription
	{
		public static readonly TableDescription Instance = new TableDescription();
		private TableDescription() : base("table", HtmlVersion.Html4, true, true) { }
		public override string Description { get { return "table; that is, data with more than one dimension."; } }
		public override Type ControlType { get { return typeof(Table); } }
		public override bool IsFlowElement { get { return true; } }

		public override bool AllowInnerText { get { return false; } }
		public override IEnumerable<HtmlElementDescription> PermittedElements
		{
			get
			{
				yield return THeadDescription.Instance;
				yield return TBodyDescription.Instance;
				yield return TFootDescription.Instance;
				yield return TrDescription.Instance;
				yield return CaptionDescription.Instance;
				yield return ColGroupDescription.Instance;
			}
		}
	}


	public class THead : TrContainerElement
	{
		public THead() : base(THeadDescription.Instance) { }
	}
	public class THeadDescription : HtmlElementDescription
	{
		public static readonly THeadDescription Instance = new THeadDescription();
		private THeadDescription() : base("thead", HtmlVersion.Html4, true, true) { }
		public override Type ControlType { get { return typeof(THead); } }
		public override string Description { get { return "block of rows that consist of the column labels (headings) for its parent table element."; } }
		public override bool AllowInnerText { get { return false; } }
		public override IEnumerable<HtmlElementDescription> PermittedParents { get { yield return TableDescription.Instance; } }
		public override IEnumerable<HtmlElementDescription> PermittedElements { get { yield return TrDescription.Instance; } }
	}


	public class TBody : TrContainerElement
	{
		public TBody() : base(TBodyDescription.Instance) { }
	}
	public class TBodyDescription : HtmlElementDescription
	{
		public static readonly TBodyDescription Instance = new TBodyDescription();
		private TBodyDescription() : base("tbody", HtmlVersion.Html4, true, true) { }
		public override Type ControlType { get { return typeof(TBody); } }
		public override string Description { get { return "block of rows that consist of a body of data for its parent table element."; } }
		public override bool AllowInnerText { get { return false; } }
		public override IEnumerable<HtmlElementDescription> PermittedParents { get { yield return TableDescription.Instance; } }
		public override IEnumerable<HtmlElementDescription> PermittedElements { get { yield return TrDescription.Instance; } }
	}


	public class TFoot : TrContainerElement
	{
		public TFoot() : base(TFootDescription.Instance) { }
	}
	public class TFootDescription : HtmlElementDescription
	{
		public static readonly TFootDescription Instance = new TFootDescription();
		private TFootDescription() : base("tfoot", HtmlVersion.Html4, true, true) { }
		public override Type ControlType { get { return typeof(TFoot); } }
		public override string Description { get { return "block of rows that consist of the column summaries (footers) for its parent table element."; } }
		public override bool AllowInnerText { get { return false; } }
		public override IEnumerable<HtmlElementDescription> PermittedParents { get { yield return TableDescription.Instance; } }
		public override IEnumerable<HtmlElementDescription> PermittedElements { get { yield return TrDescription.Instance; } }
	}
}
